#include "navigationbar.h"
#include "ui_navigationbar.h"
#include <QDebug>
#include "itemdef.h"

NavigationBar::NavigationBar(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::NavigationBar)
{
    ui->setupUi(this);

    scrollArea = new QScrollArea(parent); //创建滚动区域由
    scrollArea->setWidget(this);//滚动区域和内容窗口绑定
    scrollArea->setGeometry(0, 0, 300, 800);
    this->setGeometry(0, 0, 300, 600);//内容窗口的大小比滚动区域要大。这基本完成基本可以实现滚屏但是这种方法的不好往内容窗体sw里添加内容；
    ui->label->setGeometry(0, 0, 300, 600);

    scrollArea->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//隐藏上下滚动条
    scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);//隐藏左右滚动条
    scrollArea->setWindowFlags(Qt::FramelessWindowHint);   //设置无边框窗口
//    this->setWindowFlags(Qt::FramelessWindowHint);
    scrollArea->setFrameShape(QFrame::NoFrame);//去除边框

//    scrollArea->setParent(parent);
//    this->setParent(parent);
    scrollArea->show();

    TreeNode* pTreeNode = new TreeNode;

    pTreeNode->qsLableName = "麻精药品信息\n管理系统";//获取一级节点的lable
    pTreeNode->nLevel = 0;  //标志一级节点
    pTreeNode->collapse =  true; //标志是否展开
//    pTreeNode->nIndex = m_vecTreeNode.size();  //获取标志
    pTreeNode->iconPath = ":/new/prefix1/resource/14.png";//显示图片路径
    QStringList list;
    list<<"用户管理"<<"WIFI管理";
    //获取二级节点
    for(int i = 0; i < list.size(); i++)
    {
        TreeNode* pSecNode = new TreeNode;
        pSecNode->qsLableName = list.at(i);
        pSecNode->nLevel = 1;
//        pSecNode->nIndex = pTreeNode->listChildren.size();
        pSecNode->collapse = false;
        pTreeNode->listChildren.push_back(pSecNode);
    }

    m_vecTreeNode.push_back(pTreeNode);

    refresh();
}

NavigationBar::~NavigationBar()
{
    delete ui;

    qDeleteAll(m_vecTreeNode);
    m_vecListNode.clear();
}

void NavigationBar::refresh()
{
    m_vecListNode.clear();
    int nIndex = 0;
    length = 0;
    for(QVector<TreeNode*>::iterator it = m_vecTreeNode.begin(); it != m_vecTreeNode.end(); it++)
    {
        //一级节点
        ListNode node;
        node.qsLabelName = (*it)->qsLableName;
        node.pTreeNode = *it;
        node.pTreeNode->nIndex=nIndex;
        nIndex++;
//        m_vecListNode.push_back(node);
//        qDebug()<<__LINE__<<node.qsLabelName<<node.pTreeNode->nIndex;
        ItemDef *itemDef = new ItemDef(this);
        connect(itemDef,SIGNAL(sendItemDef(int,int)),this,SLOT(receItemDef(int,int)));
        itemDef->setGeometry(0,length,300,100);
        length+=100;
        itemDef->setLI(node.pTreeNode->nLevel,node.pTreeNode->nIndex);
        itemDef->headline1(node.qsLabelName);
        itemDef->setIcon(node.pTreeNode->iconPath);
        itemDef->show();
        node.itemDef = itemDef;
        (*it)->itemDef = itemDef;
        m_vecListNode.push_back(node);

        if(!(*it)->collapse) //如果一级节点未展开，则插入下一一级节点
            continue;

        for(QList<TreeNode*>::iterator child = (*it)->listChildren.begin(); child != (*it)->listChildren.end(); child++)
        {
            ListNode node;
            node.qsLabelName = (*child)->qsLableName;
            node.pTreeNode = *child;
            node.pTreeNode->nIndex=nIndex;
            nIndex++;
//            m_vecListNode.push_back(node);

            ItemDef *itemDef = new ItemDef(this);
            connect(itemDef,SIGNAL(sendItemDef(int,int)),this,SLOT(receItemDef(int,int)));
            itemDef->setGeometry(0,length,300,60);
            length+=60;
            itemDef->setLI(node.pTreeNode->nLevel,node.pTreeNode->nIndex);
            itemDef->headline2(node.qsLabelName);
            itemDef->setIcon(node.pTreeNode->iconPath);
            itemDef->show();
            node.itemDef = itemDef;
            (*child)->itemDef = itemDef;
            m_vecListNode.push_back(node);
//qDebug()<<__LINE__<<node.qsLabelName<<node.pTreeNode->nIndex;
        }
    }
}

void NavigationBar::refresh2()
{
    m_vecListNode.clear();
    int nIndex = 0;
    length = 0;
    for(QVector<TreeNode*>::iterator it = m_vecTreeNode.begin(); it != m_vecTreeNode.end(); it++)
    {
        //一级节点
        ListNode node;
        node.qsLabelName = (*it)->qsLableName;
        node.pTreeNode = *it;
        node.pTreeNode->nIndex=nIndex;
        nIndex++;
//        m_vecListNode.push_back(node);
//        qDebug()<<__LINE__<<node.qsLabelName<<node.pTreeNode->nIndex;
        ItemDef *itemDef = (*it)->itemDef;
//        connect(itemDef,SIGNAL(sendItemDef(int,int)),this,SLOT(receItemDef(int,int)));
        itemDef->setGeometry(0,length,300,100);
        length+=100;
        itemDef->setLI(node.pTreeNode->nLevel,node.pTreeNode->nIndex);
        itemDef->headline1(node.qsLabelName);
        itemDef->setIcon(node.pTreeNode->iconPath);
        itemDef->show();
        node.itemDef = itemDef;
        m_vecListNode.push_back(node);

        if(!(*it)->collapse) //如果一级节点未展开，则插入下一一级节点
            continue;

        for(QList<TreeNode*>::iterator child = (*it)->listChildren.begin(); child != (*it)->listChildren.end(); child++)
        {
            ListNode node;
            node.qsLabelName = (*child)->qsLableName;
            node.pTreeNode = *child;
            node.pTreeNode->nIndex=nIndex;
            nIndex++;
//            m_vecListNode.push_back(node);

            ItemDef *itemDef = (*child)->itemDef;
//            connect(itemDef,SIGNAL(sendItemDef(int,int)),this,SLOT(receItemDef(int,int)));
            itemDef->setGeometry(0,length,300,60);
            length+=60;
            itemDef->setLI(node.pTreeNode->nLevel,node.pTreeNode->nIndex);
            itemDef->headline2(node.qsLabelName);
            itemDef->setIcon(node.pTreeNode->iconPath);
            itemDef->show();
            node.itemDef = itemDef;
            m_vecListNode.push_back(node);
//qDebug()<<__LINE__<<node.qsLabelName<<node.pTreeNode->nIndex;
        }
    }
}

void NavigationBar::setGeometry2(int x, int y, int h, int w)
{
    scrollArea->setGeometry(x, y, h, w);
//    this->setGeometry(x, y, h, w);

}

void NavigationBar::receItemDef(int nLevel, int nIndex)
{
    TreeNode* pTreeNode = m_vecListNode[nIndex].pTreeNode; //获取当前点击节点

//    qDebug()<<nLevel<<nIndex<< pTreeNode->qsLableName;


    if(nLevel == 0)
    {
        for(QVector<ListNode>::iterator it = m_vecListNode.begin(); it != m_vecListNode.end(); it++)
        {
//            delete it->itemDef;
            it->itemDef->close();
        }

        //展开折叠导航栏
        pTreeNode->collapse = !pTreeNode->collapse; //刷新是否展开标志
        if(!pTreeNode->collapse)    //如果是不展开，即为展开变成合并，移除合并的
        {
            refresh2();
        }
        else
        {
            refresh2();
        }
    }
    else
    {
        //选择某个功能
        emit sendNavigationBar(pTreeNode->qsLableName);
    }

    //刷新点击样式
    setPbStyle(nIndex);

}

void NavigationBar::setPbStyle(int data)
{
    for(QVector<ListNode>::iterator it = m_vecListNode.begin(); it != m_vecListNode.end(); it++)
    {
        it->itemDef->setPbStyle("background:rgba(255, 255, 255, 0%);padding-right:4px;");
    }

    m_vecListNode[data].itemDef->setPbStyle("background: #F1F6F9;border-style: solid;border-right:4px solid #1B89CC;color: #333333;");
}

void NavigationBar::setPbStyleone(int data)
{
    m_vecListNode[data].itemDef->setPbStyle("background: #F1F6F9;border-style: solid;border-right:4px solid #1B89CC;color: #333333;");
}
